package com.tledu.springboot04.core.interceptor;

import com.tledu.springboot04.constants.CommonConstants;
import com.tledu.springboot04.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(CommonConstants.LOGIN_USER_KEY);
        if (user == null) {
            // 未登录
            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        }
        return true;
    }
}
